package Main;

import Service.*;
import Service.CardService;
import Service.UserService;
import domain.User;
import utils.Tools;

import java.io.IOException;
import java.text.ParseException;
import java.util.Scanner;

public class Main {
    private static final UserService userService = new UserService();
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException, ParseException {
        while (true) {
            System.out.println("====== 校园卡管理系统 ======");
            System.out.println("1. 登录");
            System.out.println("2. 注册");
            System.out.println("3. 修改密码");
            System.out.println("0. 退出");
            System.out.print("请选择操作：");
            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    showLoginMenu(); // 登录
                    break;
                case "2":
                    showRegisterMenu(); // 注册
                    break;
                case "3":
                    updatePassword();
                    break;
                case "0":
                    System.out.println("感谢使用，再见！");
                    return; // 退出程序
                default:
                    System.out.println("输入错误，请重新输入！");
            }
        }
    }

    private static void showLoginMenu() throws IOException, ParseException {
        System.out.print("请输入用户ID：");
        String userId = scanner.nextLine();
        System.out.print("请输入密码：");
        String password = scanner.nextLine();

        User currentUser = userService.login(userId, password);
        if (currentUser != null) {
            System.out.println("登录成功！");
            System.out.println("欢迎，" + currentUser.getName() + "！");
            switch (currentUser.getRole()) {
                case "user":
                    showUserMenu(currentUser);
                    break;
                case "clerk":
                    showClerkMenu();
                    break;
                case "admin":
                    showAdminMenu();
                    break;
                default:
                    System.out.println("未知角色！");
            }
        }
        else {
            showLoginMenu();
        }
    }

    private static void showRegisterMenu() throws IOException {
        UserService.registerUser();
    }

    private static void showUserMenu(User currentUser) throws IOException, ParseException {
        while (true) {
            System.out.println("\n===== 用户菜单 =====");
            System.out.println("1. 查询余额");
            System.out.println("2. 充值");
            System.out.println("3. 消费");
            System.out.println("4. 查看个人信息");
            System.out.println("5. 修改个人信息");
            System.out.println("0. 退出登录");
            System.out.print("请选择操作：");

            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    String userId = currentUser.getUserId(); // 假设userId = cardId
                    double balance = CardService.getBalance(userId);
                    System.out.println("当前余额为：" + balance + " 元");
                    break;
                case "2":
                    System.out.print("请输入充值金额：");
                    double rechargeAmount = Double.parseDouble(scanner.nextLine());
                    CardService.recharge(currentUser.getUserId(), rechargeAmount);
                    break;
                case "3":
                    System.out.print("请输入消费金额：");
                    double consumeAmount = Double.parseDouble(scanner.nextLine());
                    CardService.consume(currentUser.getUserId(), consumeAmount);
                    break;
                case "4":
                    UserService.queryUserInfo(currentUser.getUserId());
                    break;
                case "5":
                    UserService.updateUserInfo(currentUser);
                    break;
                case "0":
                    System.out.println("退出登录。");
                    return;
                default:
                    System.out.println("无效选择，请重试。");
            }
        }
    }

    private static void showClerkMenu() throws IOException, ParseException {
        while (true) {
            System.out.println("\n===== 卡业务员菜单 =====");
            System.out.println("1. 修改用户信息");
            System.out.println("2. 开户");
            System.out.println("3. 挂失卡");
            System.out.println("4. 冻结卡");
            System.out.println("5. 注销卡");
            System.out.println("6. 恢复卡状态");
            System.out.println("7. 查看所有消费记录");
            System.out.println("0. 退出登录");
            System.out.print("请选择操作：");

            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    User user1 = chooseUser();
                    if(user1 != null) {
                        UserService.updateUserInfo(user1);
                    }
                    break;
                case "2":
                    User user2 = chooseUser();
                    if(user2 != null) {
                        CardService.openAccount(user2.getUserId());
                    }
                    break;
                case "3":
                    updateCardStatus("挂失");
                    break;
                case "4":
                    updateCardStatus("冻结");
                    break;
                case "5":
                    updateCardStatus("注销");
                    break;
                case "6":
                    updateCardStatus("正常");
                    break;
                case "7":
                    System.out.println("请输入查询卡号：");
                    String cardId = scanner.nextLine();
                    TransactionService.viewCardTransactions(cardId);
                    break;
                case "0":
                    System.out.println("退出登录。");
                    return;
                default:
                    System.out.println("无效选择，请重试。");
            }
        }
    }

    private static void showAdminMenu() throws IOException {
        while (true) {
            System.out.println("\n===== 系统管理员菜单 =====");
            System.out.println("1. 添加用户");
            System.out.println("2. 删除用户");
            System.out.println("3. 查询用户信息");
            System.out.println("0. 退出登录");
            System.out.print("请选择操作：");

            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    UserService.addUser();
                    break;
                case "2":
                    delete();
                    break;
                case "3":
                    User user2 = chooseUser();
                    if(user2 != null) {
                        UserService.queryUserInfo(user2.getUserId());
                    }
                    break;
                case "0":
                    System.out.println("退出登录。");
                    return;
                default:
                    System.out.println("无效选择，请重试。");
            }
        }
    }

    public static User chooseUser() throws IOException{
        System.out.println("请输入操作用户id：");
        String id = scanner.nextLine();
        Tools tools = new Tools();
        User user = tools.getUser(id);
        if (user != null) {
            return user;
        }
        else {
            System.out.println("用户不存在！");
        }
        return null;
    }
    private static void updateCardStatus(String status) throws IOException {
        System.out.print("请输入卡号:");
        String cardId = scanner.nextLine();
        CardService.changeCardStatus(cardId, status);
    }
    private static void updatePassword() throws IOException {
        System.out.print("请输入账号id:");
        String userId = scanner.nextLine();
        System.out.print("请输入新密码:");
        String newPassword = scanner.nextLine();
        UserService.changePassword(userId, newPassword);
    }
    public static void delete() throws IOException {
        System.out.println("该操作会将用户以及所有卡一起删除，是否确认（y/no）？");
        String choice = scanner.nextLine();
        if (choice.equals("y")) {
            User user3 = chooseUser();
            if(user3 != null) {
                UserService.deleteUser(user3.getUserId());
            }
        }
        else{
            System.out.println("操作已取消。");
        }
    }
}